//
//  HomePage.m
//  网易新闻头部V2
//
//  Created by qianfeng on 15/9/28.
//  Copyright (c) 2015年 孔令涛. All rights reserved.
//

#import "HomePage.h"
#import "TopBarCell.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#define StatusBarHeight 20
#define NavigationHeight 44
#define TabBarHeight 49
#define ToolBarHeight 44
@interface HomePage()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>

@property (nonatomic,strong) UITableView * tableview;
@property (nonatomic,copy) NSMutableArray * dataArray;
@property (nonatomic,copy) NSMutableArray * adArray;
@property (nonatomic,weak) UIScrollView * adRunLoop;

@end
@implementation HomePage

+(id)HomePageWithIndex:(NSInteger)index UrlString:(NSString *)urlString;
{
    HomePage * topview = [[HomePage alloc]initWithFrame:CGRectMake(ScreenWidth * index, 0, ScreenWidth, ScreenHeight-64)];

    topview.urlString = urlString;

    return topview;
}

- (NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}

-(void)loadDataWith:(NSInteger)index
{
    [self.dataArray removeAllObjects];
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:_urlString parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSArray * list = dict[@"list"];
        
        for (NSDictionary * content in list) {
            TopBarModel * model = [TopBarModel modelWithDict:content];
            [_dataArray addObject:model];
        }

            [self creatSubViews];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    }];
    //下载广告滚动视图的内容
    _adArray = [[NSMutableArray alloc]init];
    NSString * loopUrl = @"http://qt.qq.com/static/pages/news/phone/c13_list_1.shtml";
    [manager GET:loopUrl parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSArray * list = dict[@"list"];
        
        for (NSDictionary * content in list) {
            TopBarModel * model = [TopBarModel modelWithDict:content];
            [_adArray addObject:model];
        }
        [self creatAdRunLoopView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    }];

    
    
}

- (void)creatSubViews
{
    _tableview = [[UITableView alloc]initWithFrame:self.frame style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    [self addSubview:_tableview];
    _tableview.tableHeaderView = _adRunLoop;
}

- (void)creatAdRunLoopView
{
    UIScrollView * adRunLoop = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 180)];
    adRunLoop.backgroundColor = [UIColor cyanColor];
    adRunLoop.contentSize = CGSizeMake(ScreenWidth * (_adArray.count+1), 180);
    adRunLoop.pagingEnabled = YES;
    adRunLoop.showsHorizontalScrollIndicator = NO;
    adRunLoop.delegate = self;
    _adRunLoop = adRunLoop;
    

    for (int i = 0 ; i < _adArray.count+1; i ++) {
        UIImageView * imgView = [[UIImageView alloc]initWithFrame:CGRectMake(i * ScreenWidth, 0, ScreenWidth, 180)];
        TopBarModel * model = nil;
        if (i == _adArray.count)
        {
            model = _adArray[0];
        }
        else
        {
            model = _adArray[i];
        }
        [imgView setImageWithURL:[NSURL URLWithString:model.image_url_big]];
        [adRunLoop addSubview:imgView];
    }
    
    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(dealRunLoop:) userInfo:adRunLoop repeats:YES];
}
- (void)dealRunLoop:(NSTimer *)timer
{
    UIScrollView * scro = timer.userInfo;
    CGPoint startPoint = scro.contentOffset;
    [scro setContentOffset: CGPointMake(startPoint.x + ScreenWidth, 0) animated:YES];
    if(scro.contentOffset.x/ScreenWidth >= _adArray.count)
        [scro setContentOffset:CGPointMake(0, 0) animated:NO];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{


}
-(void)creatTopicView
{
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TopBarCell * cell = [TopBarCell cellForRowWithTableView:tableView];
    cell.model = _dataArray[indexPath.row];
    cell.textLabel.text = @"ssdvd";
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 88;
}


@end
